
import time
import os,sys
BASE_PATH = os.path.dirname(
    os.path.dirname(os.path.abspath(__file__))
)
sys.path.insert(0,BASE_PATH)

from bin import get_Webdriver
from bin.getCheckCode import GetCheckCode
from page_object.public import Public
from conf import settings


class Regsiter_Action:
    def __init__(self):
        self.driver = get_Webdriver.Get_Webdriver().driver

    def jumpToRegsiter(self):
        """跳转到申请试用界面"""
        self.driver.open(settings.indexUrl)
        self.driver.click("css=>.apply")
        time.sleep(5)

        # curl = Public().getCurrentUrl()
        # if '/guser/register.html' not in curl:
        #     Public().setupLoginPage()
        #     self.driver.click("css=>.pull-right[onclick='toRegister();'")
        #     time.sleep(2)
        # else:
        #     self.driver.F5()
        #     time.sleep(2)

    def inputRegsiterInfo(self, companyName='', loginName='', telphoneOrMail= '', account='标准版',checkCode="获取", noClickSumbit="点击提交"):
        """输入注册信息"""
        self.choseAccountType(account)#申请试用类型
        self.inputCompanyName(companyName)#公司名称
        self.inputTelphoneOrMail(telphoneOrMail)#手机号/邮箱
        self.inputloginName(loginName)#申请人姓名
        if checkCode == "不获取":
            self.clickGetCheckCode()
        elif checkCode == "获取":
            self.clickGetCheckCode()
            if telphoneOrMail != "":
                time.sleep(2)
                checkCode = GetCheckCode().getCheckCode(telphoneOrMail, token="notoken")
                self.inputCheckCode(checkCode)
                print("checkCodeid---{}".format(checkCode))
        else:
            self.inputCheckCode(checkCode)
        if noClickSumbit == "点击提交":
            self.clickSumbit()  # 点击注册

    def choseAccountType(self, accountType):
        if accountType == '标准版':
            self.driver.click("css=>#standard>[onclick='applyV(1)']")
        if accountType == '专业版':
            self.driver.click("css=>#standard>[onclick='applyV(3)']")
        if accountType == '数据中心版':
            self.driver.click("css=>#standard>[onclick='applyV(5)']")

    def inputCompanyName(self, companyName):
        """输入企业名称"""
        self.driver.clear("css=>#companyname")
        self.driver.type("css=>#companyname", companyName)

    def inputTelphoneOrMail(self, telphoneOrMail):
        """输入手机号或邮箱"""
        self.driver.clear("css=>#contactway")
        self.driver.type("css=>#contactway", telphoneOrMail)

    def inputloginName(self,loginName):
        self.driver.clear("css=>#name")
        self.driver.type("css=>#name", loginName)

    def clickGetCheckCode(self):
        """点击获取验证码"""
        self.driver.click("css=>#applyProbationSend")

    def inputCheckCode(self, checkCode):
        """输入验证码"""
        self.driver.clear("css=>#validateCode")
        self.driver.type("css=>#validateCode", checkCode)

    def clickSumbit(self):
        self.driver.click("css=>button[onclick='applyProbation();']")

    def getRegsiterResult(self):
        """获取注册的结果  即用户信息回显"""
        regsiterResult = self.driver.get_text("xpath=>.//*[@class='pull-left register-ul']")
        time.sleep(0.5)
        self.driver.click("xpath=>.//*[@onclick='toLogin()']")
        return regsiterResult

    def getErrorMsg(self):
        """获取错误提示"""
        errorMsg = ''
        errorMsg = errorMsg + self.driver.get_text("css=>#companynameError")
        errorMsg = errorMsg + self.driver.get_text("css=>#nameError")
        errorMsg = errorMsg + self.driver.get_text("css=>#validateCodeError")
        errorMsg = errorMsg + self.driver.get_text("css=>#contactwayError")
        return errorMsg

    # def isRegsiterPage(self):
    #     return (self.driver.get_url == self.url_test_offical)

    # def getErrorMsg2(self):
    #     """获取错误提示2  仅用于手机号码重复"""
    #     errorMsg = self.dr.find_element_by_xpath(".//*[@id='contactwayError']").text
    #     return errorMsg


if __name__=="__main__":
    # checkCode = getCheckCode.GetCheckCode().getCheckCode("18888888876")
    # print(checkCode)
    Regsiter_Action().jumpToRegsiter()
    Regsiter_Action().inputRegsiterInfo("公司名称","申请人姓名","18888888871")
    # print(RegsiterAction().getRegsiterResult())